﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Linq;

namespace Components
{
    public class EmailInstruction:InstructionBase
    {
        #region props
        public bool UseOutlook { get; set; }
        public string SmtpServer { get; set; }
        public string FromAddress { get; set; }
        public string ToAddress { get; set; }
        public string Subject { get; set; }
        public bool RequireAuthentication { get; set; }
        public string UserName { get; set; }
        public string Password { get; set; }
        public bool EnableSSL { get; set; }
        public List<string> Attachments { get; set; }
        public string Body { get; set; }
        #endregion

        #region ctor
        public EmailInstruction():base(Guid.Empty,Guid.Empty)
        {
            this.UseOutlook = false;
            this.SmtpServer = string.Empty;
            this.FromAddress = string.Empty;
            this.ToAddress = string.Empty;
            this.Subject = string.Empty;
            this.RequireAuthentication = false;
            this.UserName = string.Empty;
            this.Password = string.Empty;
            this.EnableSSL = false;
            this.Attachments=new List<string>();
            this.Body = string.Empty;
        }

        public EmailInstruction(Guid componentID, Guid ticketID,
            bool useOutlook, string smtpServer, 
            string fromAddr, string toAddr,
            string subj, bool auth,
            string userName, string password,
            bool enableSSL, List<string> attachments,
            string body):base(componentID, ticketID)
        {
            this.UseOutlook = useOutlook;
            this.SmtpServer = smtpServer;
            this.FromAddress = fromAddr;
            this.ToAddress = toAddr;
            this.Subject = subj;
            this.RequireAuthentication = auth;
            this.UserName = userName;
            this.Password = password;
            this.EnableSSL = enableSSL;
            this.Attachments = attachments;
            this.Body = body;
        }
        #endregion

        #region Overrides of InstructionBase

        public override XElement Serialize()
        {
            XElement xe = base.Serialize();
            xe.Add(new XAttribute("UseOutlook", this.UseOutlook.ToString()));
            xe.Add(new XAttribute("FromAddress", this.FromAddress));
            xe.Add(new XAttribute("ToAddress", this.ToAddress));
            xe.Add(new XAttribute("Subject", this.Subject));
            xe.Add(new XAttribute("RequireAuthentication", this.RequireAuthentication.ToString()));
            xe.Add(new XAttribute("UserName", this.UserName));
            xe.Add(new XAttribute("Password", this.Password));
            xe.Add(new XAttribute("EnableSSL", this.EnableSSL.ToString()));
            xe.Add(new XAttribute("Body", this.Body));
            if(this.Attachments !=null && this.Attachments.Count>0)
            {
                XElement attachments=new XElement("Attachments");
                xe.Add(attachments);
                foreach(string filePath in this.Attachments)
                {
                    XElement attachment=new XElement("Attachment",
                        new XAttribute("FilePath",filePath));
                    attachments.Add(attachment);
                }
            }
            return xe;
        }

        public override InstructionBase Instantiate(XElement xe)
        {
            EmailInstruction instruction = (EmailInstruction) base.Instantiate(xe);
            instruction.UseOutlook = bool.Parse(xe.Attribute("UseOutlook").Value);
            instruction.FromAddress = xe.Attribute("FromAddress").Value;
            instruction.ToAddress = xe.Attribute("ToAddress").Value;
            instruction.Subject = xe.Attribute("Subject").Value;
            instruction.RequireAuthentication = bool.Parse(xe.Attribute("RequireAuthentication").Value);
            instruction.UserName = xe.Attribute("UserName").Value;
            instruction.Password = xe.Attribute("Password").Value;
            instruction.EnableSSL = bool.Parse(xe.Attribute("EnableSSL").Value);
            instruction.Body = xe.Attribute("Body").Value;
            foreach(XElement attachment in xe.Elements("Attachments").Elements("Attachment"))
            {
                instruction.Attachments.Add(attachment.Attribute("FilePath").Value);
            }
            return instruction;
        }

        #endregion
    }
}
